package cliente;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import acessoBancoDeDados.AcessoBancoDados;
import endereco.Endereco;

public class RepositorioBanco implements IRepositorioCliente {

	Connection c1 = AcessoBancoDados.AcessarBancoDeDados();	
	
	public void cadastrarCliente(Cliente cliente, Endereco end) throws SQLException{
		
	PreparedStatement stmts = c1.prepareStatement("INSERT INTO cli_cliente (cli_nome,cli_rg,cli_cpf, " +
			"cli_email, cli_logradouro, cli_numero,cli_cep) VALUES(?,?,?,?,?,?,?)");
		stmts.setString(1, cliente.getNome());
		stmts.setString(3, cliente.getRg());
		stmts.setString(2, cliente.getCpf());
		stmts.setString(4, cliente.getEmail());
		stmts.setString(5, end.getLogradouro());
		stmts.setString(6, end.getNumero());
		stmts.setString(7, end.getCep());
		
		stmts.executeUpdate();
		
	}

	
	public Collection<Cliente> PesquisarTodos() throws SQLException {
		
		Collection<Cliente> clie = new ArrayList<Cliente>();
		String sql = "select * from cli_cliente";
		
		PreparedStatement stmst = c1.prepareStatement(sql);
		
		ResultSet rs = stmst.executeQuery();
		
		Cliente cliente = new Cliente();
		while(rs.next()){
			cliente.setNome(rs.getString("cli_nome"));
			cliente.setEmail(rs.getString("cli_email"));
			clie.add(cliente);
		}
		return clie;
}

	public Cliente PesquisarPorNome(String clientePesquisa) throws SQLException {
				
		String sql = "select * from cli_cliente where cli_nome = ?";
		
		PreparedStatement stmts = c1.prepareStatement(sql);
		
		stmts.setString(1, clientePesquisa);
		ResultSet rs = stmts.executeQuery();
		
		Cliente cliente = new Cliente();
		while(rs.next()){
			cliente.setNome(rs.getString("cli_nome"));
			cliente.setEmail(rs.getString("cli_email"));
		}
				
		return cliente;
	}

	
	public void alterarCliente(Cliente alterarCliente) throws SQLException {
		
		Connection conec = AcessoBancoDados.AcessarBancoDeDados();

		
		PreparedStatement stmts = conec.prepareStatement("INSERT INTO tab_cliente (cli_nome,cli_cpf,cli_rg, " +
				"cli_email, cli_logradouro, cli_numero,cli_cep,cli_rua) VALUES(?,?,?,?,?,?,?,?)");
			stmts.setString(1, alterarCliente.getNome());
			stmts.setString(2, alterarCliente.getCpf());
			stmts.setString(3, alterarCliente.getRg());
			stmts.setString(4, alterarCliente.getEmail());
			stmts.setString(5, alterarCliente.getEndereco().getLogradouro());
			stmts.setString(6, alterarCliente.getEndereco().getNumero());
			stmts.setString(7, alterarCliente.getEndereco().getCep());
		
		stmts.executeUpdate();
	}

	
	public void excluirCli(int id) throws SQLException {
		
		Connection c4 = AcessoBancoDados.AcessarBancoDeDados();
		
		PreparedStatement psmt = c4.prepareStatement("DELETE FROM tab_cliente WHERE cli_codcliente = ?");
		
		psmt.setInt(1, id);
		psmt.executeUpdate();
		
		
		
	}

}
